#pragma once

#include "fxcc/core/platform/common/pch.h"

namespace fxcc
{
    namespace platform
    {
        namespace common
        {
            struct FXCC_API Keyboard
            {
                struct FXCC_API _Ev
                {
                    KeyCode m_KeyCode;
                    ActionType m_Type;
                };
                
                std::vector<_Ev> m_Evs;

                bool m_States[(int)KeyCode::COUNT];

                Keyboard()
                {
                    memset(m_States, 0, sizeof(m_States));
                }
                void Callback(const KeyCode& keyCode, const ActionType & actionType)
                {
                    m_States[(int)keyCode] = (bool)actionType;
                    this->m_Evs.push_back({ keyCode, actionType });
                }
                bool IsPressing(const KeyCode& keyCode) const
                {
                    return m_States[(int)keyCode];
                }
                bool HasKeyDown(const KeyCode& keyCode) const
                {
                    return HasEv(keyCode, ActionType::Down);
                }
                bool HasKeyUp(const KeyCode& keyCode)const
                {
                    return HasEv(keyCode, ActionType::Up);
                }
                bool HasEv(const KeyCode& keyCode, const ActionType& actionType) const
                {
                    for (const auto& it : m_Evs)
                    {
                        if (it.m_KeyCode == keyCode && it.m_Type == actionType) { return true; }
                    }
                    return false;
                }
                glm::vec2 GetAxis() const
                {
                    glm::vec2 axis(0.0f);

                    if (IsPressing(KeyCode::W) || IsPressing(KeyCode::UP))
                    {
                        axis.y = 1.0f;
                    }
                    if (IsPressing(KeyCode::S) || IsPressing(KeyCode::DOWN))
                    {
                        axis.y = -1.f;
                    }
                    if (IsPressing(KeyCode::D) || IsPressing(KeyCode::RIGHT))
                    {
                        axis.x = 1;
                    }
                    if (IsPressing(KeyCode::A) || IsPressing(KeyCode::LEFT))
                    {
                        axis.x = -1;
                    }
                    if (glm::length(axis) == 0)
                        return axis;
                    else
                        return glm::normalize(axis);
                }
                void Clear()
                {
                    m_Evs.clear();
                }
            };
        };
    };
};
